

var SiteNote; if (!SiteNote) SiteNote = {};

var ratio;

//alert('test');
function startResize()
{
	//alert('ici');
	ratio = window.event.srcElement.width / window.event.srcElement.height;

}
function endResize()
{
	if (window.event.shiftKey == true)
	{
		window.event.srcElement.style.height = window.event.srcElement.width / ratio;

	}
}

function Initialize()
{
	SiteNoteEditor = new SiteNoteEditor();
}

function SiteNoteEditor()
{
	this.xmlHttp = null;
	this.app = "test";
}

SiteNoteEditor.prototype.OpenLibrary = function(app, id)
{
	var returnValue;
	var content;
	var win;
	var oEditor;
	var oSel;
	var extention;
	
	returnValue = window.showModalDialog(app + "/assetmanager/assetmanager.asp",window,"dialogWidth:640px;dialogHeight:465px;edge:Raised;center:Yes;help:No;Resizable:Yes;Maximize:Yes");
	if(returnValue != null)
	{
		//alert(returnValue);
		extention = returnValue.split(".");
		//alert(extention.length);
		if(extention.length > 0)
		{
			//alert(extention[extention.length-1]);
			switch(extention[extention.length-1])
			{
				case "gif":
				case "jpeg":
				case "bmp":
				case "png":
				case "jpg":
					content = "<img src=\"" + returnValue + "\">";
					break;
				default:
					content = "<a href=\"" + returnValue + "\">" + returnValue + "</a>";
					break;
			}
			oEditor = eval("idContentoEdit1_" + id);
			oSel = oEditor.document.selection.createRange();
			if(oSel.htmlText != "<SPAN></SPAN>")
			{
				oSel.pasteHTML(content);
			}
		}		
	}
	return returnValue;
}

SiteNoteEditor.prototype.Initialize = function()
{
	/*alert('hjgfljkf');*/
	try
	{
		this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		if(this.root != undefined && this.root.debug == "True")
		{
			SiteNote.Debug.error("Function Initialize Error\n" + e.message);
		}
		this.xmlHttp = null;
	}
	return (this.xmlHttp != null);
}

SiteNoteEditor.prototype.Edit = function(id)
{
	var obj;
	var SiteNoteEditorDiv;
	var htmlContent;
	var objEditor;
	var objEditButton;
		
	obj = document.all[id];
	SiteNoteEditorDiv = document.all["SiteNoteEditor_" + id];
	objEditor = eval("oEdit1_" + id);
	objContent = document.all["Content_" + id];
	
	if(obj != undefined && SiteNoteEditorDiv != undefined)
	{
		switch(obj.SiteNoteContentType)
		{
			case 'html':
				objEditButton = document.all["EditButton_" + id];
				if(objEditor != undefined && objEditor != null)
				{
					SiteNoteEditorDiv.style.display = '';
					//alert(obj.offsetHeight);
					htmlContent = obj.innerHTML;
					try
					{
						//objEditor.Height = obj.offsetHeight;
						//objEditor.Height = '800px';
						
						objEditor.loadHTML(htmlContent);
						//objEditor.putHTML(htmlContent);
					}
					catch(e)
					{
						//alert('Load HTML Error');
					}
					obj.style.display = 'none';
					if(objEditButton != undefined && objEditButton != null)
					{
						objEditButton.style.display = 'none';
					}
				}
				break;
			default:
				break;
		}
	}
}

SiteNoteEditor.prototype.CancelEdit = function(id)
{
	var SiteNoteEditorDiv;
	var objEditor;
	var obj;
	var objEditButton;
	
	obj = document.all[id];
	objEditor = eval("oEdit1_" + id);
	SiteNoteEditorDiv = document.all["SiteNoteEditor_" + id];
	objEditButton = document.all["EditButton_" + id];
	
	if(SiteNoteEditorDiv != undefined)
	{
		if(objEditor != undefined)
		{
			try
			{
				objEditor.putHTML("");
			}
			catch(e)
			{
			}
			SiteNoteEditorDiv.style.display = 'none';
			obj.style.display = '';
			objEditButton.style.display = '';
		}
	}
}

SiteNoteEditor.prototype.SaveHtmlContent = function(app, id)
{
	var obj;
	var htmlContent;
	var SiteNoteEditorDiv;
	var status;
	var doc;
	var post;
	var response;
	var responseDocument;
	var pageUrl;
	var test;
	var re;
	var src;
	var range;
	var objEditor;
	var objEditButton;
	
	pageUrl = sourcePageName;
		
	SiteNoteEditorDiv = document.all["SiteNoteEditor_" + id];
	obj = document.all[id];
	objEditor = eval("oEdit1_" + id);
	objEditButton = document.all["EditButton_" + id];
	if(obj != undefined && SiteNoteEditorDiv != undefined && objEditor != undefined)
	{	
		htmlContent = objEditor.getHTMLBody();
		//alert(htmlContent);
		obj.innerHTML = htmlContent;
		obj.style.display = '';
		post = "content=" + encodeURIComponent(htmlContent) + "&pageUrl=" + encodeURIComponent(pageUrl) + "&contentName=" + id;

        var url = app + "/Admin/Webservices/SecurePageSvc.asmx";
        var wsClient = new WSClient(url, "POST", "http://tempuri.org/");
		wsClient.setSoapHeader("SessionIdHeader", {Id: SiteNote.Session.SessionId});
        response = wsClient.invoke("SaveDraft" , {content: htmlContent, pageUrl: pageUrl, contentName: id});

		if (wsClient.getErrorCode() != 0)
		{
            SiteNote.Debug.error('SaveHtmlContent - unable to execute request');
		}
		else
		{
			/* MsXml modif */
			responseDocument = new ActiveXObject("Msxml2.DOMDocument");
			
			responseDocument.loadXML(response);
			
			if (responseDocument.parseError.errorCode != 0 || responseDocument.xml == '')
			{
				SiteNote.Debug.error("[SiteNote][SaveHtmlContent] Unable to read response");
			}
			else
			{
				status = responseDocument.documentElement.firstChild;

				if (status.text == 'succeed')
				{
					//alert('save draft succeed');
					objEditButton.style.display = '';
				}
				else
				{
					SiteNote.Debug.error("[SiteNote][SaveHtmlContent] Save draft failed");
				}
			}
		}		
	}
	SiteNoteEditorDiv.style.display = 'none';
}

function test()
{
	//alert('test');
}

function HideIcon(action, obj)
{
	var ids;
	var id;
	alert('HideIcon');
	alert(obj.id);
	alert(action);
	ids = obj.id.split('_');
	obj.style.display = 'none';
	if(action == "edit")
	{
		id = ids[0] + "_" + ids[1] + "_save"; 
	}
	else
	{
		id = ids[0] + "_" + ids[1] + "_edit"; 
	}
	obj = document.all[id];
	if(obj != undefined && obj != null)
	{
		obj.style.display = '';
	}
}